function [] = particle_processor_fast_h5(timestamp,totpart,runname)

%11/5/13 MTK (original)
%Function created to read in flexpart/pilt particle position info

%8/11/16 MTK (modification)
%Read in particle number (dumped in first column of partposit file when MQUASILAG=1 in
%pilt.input).  User provides the total number of particles, used to
%initialize arrays.

%12/5/16 MTK (modification)
%Modified script to increase speed.  (switched from "dlmread" to "textscan")
%Script runs ~13-15 times faster now.  I also cleaned up unused lines of code.


%construct structure array with names of all particle files.
if(timestamp == 0 )
   files=dir('*partposit_2*');   
else    
   srch=strcat('*partposit_',num2str(timestamp),'*');
   files=dir(srch);   

   if(size(files) == 0)
       exit
   end
   
end

%Pre-allocate arrays for later.
TOPO_2D=nan(totpart,size(files,1));%dimensions: particle no. x time
ELEV_2D=nan(totpart,size(files,1));%dimensions: particle no. x time
LAT_2D=nan(totpart,size(files,1));%dimensions: particle no. x time
LON_2D=nan(totpart,size(files,1));%dimensions: particle no. x time
AGE_2D=nan(totpart,size(files,1));%dimensions: particle no. x time
TEMP_2D=nan(totpart,size(files,1));%dimensions: particle no. x time
TIME_1D=nan(1,size(files,1));%dimension: time

%loop over all files, reading the data into arrays.
for f=1:size(files,1)

    message=strcat({'Reading in file: '},files(f).name);
    disp(message)
    
    filename=files(f).name;
    delimiter = ' ';
    formatSpec = '%f%f%f%f%f%f%f%f%f%f%f%f%f%[^\n\r]';
    
    % Open the text file.
    fileID = fopen(filename,'r');
    
    % Read columns of data according to format string.
    dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'EmptyValue' ,NaN, 'ReturnOnError', false);
    
    % Close the text file.
    fclose(fileID);
    
    %% Create output variable
    tem = [dataArray{1:end-1}];
    %% Clear temporary variables
    clearvars filename delimiter formatSpec fileID dataArray ans;

     TIME=tem(1,1);    
     TIME_1D(1,f)=TIME;
     for r=2:size(tem,1)-1%Skip header and footer lines.
        AGE_2D(tem(r,1),f)=(TIME-tem(r,2))/60.;
        %Age of particle in minutes, calculated from particle release time, in seconds 
        %after PILT init [tem(r,2)] and current time, in seconds after PILT init.         
        LON_2D(tem(r,1),f)=tem(r,3);%degrees longitude 
        LAT_2D(tem(r,1),f)=tem(r,4);%degrees latitude  
        ELEV_2D(tem(r,1),f)=tem(r,5);%elevation agl (m)   
        TOPO_2D(tem(r,1),f)=tem(r,6);%surface elevation amsl (m)
	TEMP_2D(tem(r,1),f)=tem(r,12);%temperature [K] 
     end


end
  
%save(strcat('partposit_',runname,'.mat'),'ELEV_2D','LAT_2D','LON_2D','AGE_2D','TOPO_2D','TIME_1D','-v7.3')
chunk_1=min(size(AGE_2D,1),100);chunk_2=min(size(AGE_2D,2),10);
h5create(strcat('partposit_',runname,'_withTEMP.h5'),'/ELEV_2D',[size(AGE_2D,1) size(AGE_2D,2)],'Chunksize',[chunk_1 chunk_2],'Deflate',9);
h5create(strcat('partposit_',runname,'_withTEMP.h5'),'/LAT_2D',[size(AGE_2D,1) size(AGE_2D,2)],'Chunksize',[chunk_1 chunk_2],'Deflate',9);
h5create(strcat('partposit_',runname,'_withTEMP.h5'),'/LON_2D',[size(AGE_2D,1) size(AGE_2D,2)],'Chunksize',[chunk_1 chunk_2],'Deflate',9);
h5create(strcat('partposit_',runname,'_withTEMP.h5'),'/AGE_2D',[size(AGE_2D,1) size(AGE_2D,2)],'Chunksize',[chunk_1 chunk_2],'Deflate',9);
h5create(strcat('partposit_',runname,'_withTEMP.h5'),'/TOPO_2D',[size(AGE_2D,1) size(AGE_2D,2)],'Chunksize',[chunk_1 chunk_2],'Deflate',9);
h5create(strcat('partposit_',runname,'_withTEMP.h5'),'/TEMP_2D',[size(AGE_2D,1) size(AGE_2D,2)],'Chunksize',[chunk_1 chunk_2],'Deflate',9);
h5create(strcat('partposit_',runname,'_withTEMP.h5'),'/TIME_1D',[1 size(AGE_2D,2)],'Chunksize',[1 chunk_2],'Deflate',9);
h5write(strcat('partposit_',runname,'_withTEMP.h5'),'/ELEV_2D',ELEV_2D);
h5write(strcat('partposit_',runname,'_withTEMP.h5'),'/LAT_2D',LAT_2D);
h5write(strcat('partposit_',runname,'_withTEMP.h5'),'/LON_2D',LON_2D);
h5write(strcat('partposit_',runname,'_withTEMP.h5'),'/AGE_2D',AGE_2D);
h5write(strcat('partposit_',runname,'_withTEMP.h5'),'/TOPO_2D',TOPO_2D);
h5write(strcat('partposit_',runname,'_withTEMP.h5'),'/TEMP_2D',TEMP_2D);
h5write(strcat('partposit_',runname,'_withTEMP.h5'),'/TIME_1D',TIME_1D);


disp('Done!')

end           
             
             
